<?php
/**
 * Created by PhpStorm.
 * User: Tin
 * Date: 6/27/14
 * Time: 10:18 PM
 */

namespace App\Library\System\Model;

use App\Library\System\Model;

/**
 * Cache Model
 *
 * @category   Model
 * @package    App\Library\System\Model
 * @author     Tin Nguyen <tinntt@peslook.com>
 * @copyright  Peslook
 * @version    1.0
 * @since      Class available since Release 1.0
 */
class CacheModel extends Model
{
    private static $static_style;

    public static function getInstance()
    {
        if (!self::$static_style) {
            self::$static_style = new CacheModel();
        }

        return self::$static_style;
    }

    /**
     * Create cache
     *
     * @param $type
     * @param $name
     * @param $content
     */
    public function createCache($type, $name, $content)
    {
        if ($this->checkCache($type, $name) != null)
            $this->non($this->update()
                ->table('cache')
                ->set(array('content' => $content))
                ->where(array('type' => $type,
                              'name' => $name)));

        else
            $this->non($this->insert()
                ->into('cache')
                ->values(array(
                        'type'    => $type,
                        'name'    => $name,
                        'content' => $content
                    )
                ));
    }

    /**
     * Load Cache (get content) from database with type and name of Cache
     *
     * @param $type
     * @param $name
     *
     * @return mixed
     */
    public function loadCache($type, $name)
    {
        return $this->one($this->select()
            ->from('cache')
            ->columns(array('content'))
            ->where(array(
                    'type' => $type,
                    'name' => $name
                )
            ))->content;
    }

    /**
     * Check exist Cache in database
     *
     * @param $type
     * @param $name
     *
     * @return array|\ArrayObject|null|\Zend\Db\ResultSet\ResultSet
     */
    public function checkCache($type, $name)
    {
        return $this->one($this->select()
            ->from('cache')
            ->columns(array('id'))
            ->where(array(
                    'type' => $type,
                    'name' => $name
                )
            ));
    }

    // Get current path
    public function getPath()
    {
        return '/';
    }

    public function getUID()
    {
        return 0;
    }

    public function getLanguage()
    {
        return $_SESSION['app_lang'];
    }

    public function getCacheHash()
    {
        $str = 'lang=' . $this->getLanguage() . ';path=' . $this->getPath() . ';uid=' . $this->getUID();

        return md5($str);
    }

    public function getCache($type, $name)
    {
        return $this->one($this->select()
                ->from('cache')
                ->columns(array('content'))
                ->where(
                    array(
                        'type' => $type,
                        'name' => $name
                    )
                )
        );
    }

    public function canCompress()
    {
        return (@strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== false);
    }
} 